<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">编号</label>
        <div class="layui-input-block">
            <input type="test" name="depId" lay-verify="required" readonly="readonly" value="" class="layui-input" style="width: 30%;">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="depName" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入用户名" value=""
                   class="layui-input" style="width: 30%;">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">负责人</label>
        <div class="layui-input-inline">
            <input type="text" name="leader" placeholder="请输入负责人" value="" class="layui-input" id="leader" autocomplete="off" style="width: 237%;">
            <input type="hidden" name="leaderId" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">副负责人</label>
        <div class="layui-input-inline">
            <input type="text" name="dleader" placeholder="请输入副负责人" value="" class="layui-input" id="dleader" autocomplete="off" style="width: 237%;">
            <input type="hidden" name="dleaderId" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息" style="width: 30%;"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form','table','tableSelect'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            tableSelect = layui.tableSelect;
        $(function () {
            var url = location.search;
            var i = url.indexOf("=");
            var id = url.substring(i + 1);
            // $.post("/dep/getDep.do", {depId: id},function (data) {
            //     $("input[name='depId']").attr("value", data.data.depId);
            //     $("input[name='depName']").attr("value", data.data.depName);
            //     $("input[name='remark']").attr("value", data.data.remark);
            //     // $("input[name='depId']").attr("value", data.data);
            //     // $("input[name='depId']").attr("value", data.data);
            // }, "json");
            $.ajax({
                type: "post",
                url: "/dep/getDep.do",
                data: {
                    depId: id
                },
                success: function (data) {
                    if(data.code==1){
                        alert(data.msg);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }else if(data.code==0){
                        $("input[name='depId']").attr("value", data.data.dep.depId);
                        $("input[name='depName']").attr("value", data.data.dep.depName);
                        $("input[name='remark']").attr("value", data.data.dep.remark);
                        $("input[name='leader']").attr("value", data.data.leader.empName);
                        $("input[name='leaderId']").attr("value", data.data.leader.empId);
                        var dleader = [];
                        var dleaderId = [];
                        for (var i = 0;i<data.data.dleader.length;i++){
                            dleader.push(data.data.dleader[i].empName);
                            dleaderId.push(data.data.dleader[i].empId);
                        }
                        dleader.join(",")
                        $("input[name='dleader']").attr("value", dleader.toString());
                        $("input[name='dleaderId']").attr("value", dleaderId);
                    }
                }
            });
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                type: "post",
                url: "/dep/edit.do",
                data: {
                    depId: data.field.depId,
                    depName: data.field.depName,
                    description: data.field.remark,
                    leader: data.field.leaderId,
                    dleader: data.field.dleaderId
                },
                success: function (data) {
                    if (data.code == 0) {
                        alert(data.msg);
                        //先得到当前iframe层的索引
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        //刷新父级页面的表格
                        parent.layui.table.reload('currentTableId');
                    } else if (data.code == 1) {
                        alert(data.msg);
                    }
                }
            });
            return false;
        });
        tableSelect.render({
            elem: '#dleader',
            checkedKey: 'id',
            height: '300',
            width: '900',
            searchType: 'more',
            searchList: [
                {searchKey: 'empName', searchPlaceholder: '搜索员工姓名'},
                {searchKey: 'depName', searchPlaceholder: '搜索员工部门'},
            ],
            table: {
                url: '/emp/selectEmp.do',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'empId', title: '编号'},
                    {field: 'empName', title: '姓名'},
                    {field: 'depName', title: '部门',templet:'<div>{{d.dep.depName}}</div>'},
                    {field: 'postName', title: '职位',templet:'<div>{{d.post.postName}}</div>'}
                ]]
            },
            done: function (elem, data) {
                var list = []
                var NEWJSON = []
                layui.each(data.data, function (index, item) {
                    list.push(item.empId)
                    NEWJSON.push(item.empName)
                })
                elem.val(NEWJSON.join(","))
                $("input[name='dleaderId']").attr("value", list);
            }
        })

        tableSelect.render({
            elem: '#leader',
            checkedKey: 'empId',
            height: '300',
            width: '900',
            searchType: 'more',
            searchList: [
                {searchKey: 'empName', searchPlaceholder: '搜索员工姓名'},
                {searchKey: 'depName', searchPlaceholder: '搜索员工部门'},
                // {searchKey: 'post', searchPlaceholder: '搜索员工岗位'},
            ],
            table: {
                url: '/emp/selectEmp.do',
                cols: [[
                    {type: 'radio'},
                    {field: 'empId', title: '编号'},
                    {field: 'empName', title: '姓名'},
                    {field: 'depName', title: '部门',templet:'<div>{{d.dep.depName}}</div>'},
                    {field: 'postName', title: '职位',templet:'<div>{{d.post.postName}}</div>'}
                ]]
            },
            done: function (elem, data) {
                var NEWJSON = []
                var massage;
                var re;
                layui.each(data.data, function (index, item) {
                    if(item.post.postRank>=24){
                        re = true;
                        massage = item.empName+"是"+item.post.postName;
                    }else if(item.post.postRank>5){
                        re = true;
                        massage = item.empName+"是"+item.dep.depName+"的"+item.post.postName;
                    }
                    NEWJSON.push(item.empName)
                    $("input[name='leaderId']").attr("value", item.empId);
                })
                massage+=",确认任命"
                if(re){
                    layer.open({
                        title:['确认消息'],
                        content: massage,
                        btn: ['确定','取消'],
                        shadeClose: true,
                        yes: function(index, layer){
                            // 关闭弹出层
                            layer.close(index);
                            elem.val(NEWJSON.join(","));
                        },
                        no: function (){
                            layer.close(index);
                        }
                    })
                }else {
                    elem.val(NEWJSON.join(","))
                }

            }
        })
    });
</script>
</body>
</html>